package employee.web.beans;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.primefaces.event.RowEditEvent;

import dtos.ExcursionDTO;
import dtos.HotelDTO;
import employee.ejb.management.ProductMgr;


@SuppressWarnings("serial")
@ManagedBean(name="listBean")
@ViewScoped
public class ListBean implements Serializable {  
       
    @EJB
	private ProductMgr proMgr;
         
    private HotelDTO selectedHotel; 
    private ExcursionDTO selectedExcursion;
	private DataModel<HotelDTO> _hotelModel;
	private DataModel<ExcursionDTO> _excModel;
    

    @PostConstruct
	public void refresh()
	{
    	refreshExcursion();
    	refreshHotel();
	}
	
    public void refreshHotel(){
    	List<HotelDTO> h = new LinkedList<>();
    	for(HotelDTO hotel : proMgr.getHotelList())
			h.add(hotel);
    	_hotelModel = new ListDataModel<>(h);
    }
    
    public void refreshExcursion(){
    	List<ExcursionDTO> e = new LinkedList<>();
    	for(ExcursionDTO exc : proMgr.getExcursionList())
			e.add(exc);
    	_excModel = new ListDataModel<>(e);

    }
    
    public HotelDTO getSelectedHotel() {  
        return selectedHotel;  
    }  
  
    public void setSelectedHotel(HotelDTO selectedHotel) {  
        this.selectedHotel = selectedHotel;  
    }
    
    public ExcursionDTO getSelectedExcursion(){
    	return selectedExcursion;
    }
    
    public void setSelectedExcursion(ExcursionDTO selectedExcursion){
    	this.selectedExcursion = selectedExcursion;
    }
    

	public DataModel<HotelDTO> getHotelModel()
	{
		refreshHotel();
		return _hotelModel;
	
	}
	
	public DataModel<ExcursionDTO> getExcModel(){
		refreshExcursion();
		return _excModel;
	}
	
	public void onEditHotel(RowEditEvent event) { 
		HotelDTO hdto = new HotelDTO();
		hdto.setHotelID(((HotelDTO) event.getObject()).getHotelID());
		hdto.setName(((HotelDTO) event.getObject()).getName());
		hdto.setArrivalPlace(((HotelDTO) event.getObject()).getArrivalPlace());
		hdto.setClassification(((HotelDTO) event.getObject()).getClassification());
		hdto.setCost(((HotelDTO) event.getObject()).getCost());
		hdto.setStreetName(((HotelDTO) event.getObject()).getStreetName());
		hdto.setStreetNumber(((HotelDTO) event.getObject()).getStreetNumber());
		
		proMgr.updateHotel(hdto);
		
        FacesMessage msg = new FacesMessage("Hotel Edited");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    } 
	
	public void onEditExcursion(RowEditEvent event) { 
		ExcursionDTO edto = new ExcursionDTO();
		edto.setExcursionID(((ExcursionDTO) event.getObject()).getExcursionID());
		edto.setName(((ExcursionDTO) event.getObject()).getName());
		edto.setArrivalPlace(((ExcursionDTO) event.getObject()).getArrivalPlace());
		edto.setMeetingPlace(((ExcursionDTO) event.getObject()).getMeetingPlace());
		edto.setCost(((ExcursionDTO) event.getObject()).getCost());
		edto.setMeetingTime(((ExcursionDTO) event.getObject()).getMeetingTime());
		
		proMgr.updateExcursion(edto);
		
        FacesMessage msg = new FacesMessage("Excursion Edited");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
      
    public void onCancel(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Action Cancelled");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }

}